using System;
using System.Text;


namespace FW
{
    public class crypt
    {

        public crypt()
        { }

        ~crypt()
        { }

        public string encrypt(string chave, string valor)
        {
            try
            {
                string temp = "";
                if (chave.Length != 0 && valor.Length != 0)
                {
                    byte[] codVal = System.Text.ASCIIEncoding.Default.GetBytes(valor);
                    byte[] codCha = System.Text.ASCIIEncoding.Default.GetBytes(chave);


                    int tloop = codVal.Length;
                    int tzera = codCha.Length;
                    int cont = 0;
                    for (int i = 0; i < tloop; i++)
                    {
                        temp += (char)Convert.ToInt32((codVal[i] ^ codCha[cont]));
                        cont++;
                        cont = (cont == tzera) ? 0 : cont;
                    }
                }

                return temp;
            }
            catch (Exception e)
            {
                throw (e);
            }
        }

        public string encryptMD5(string pwd)
        { 
        	System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(pwd);
            byte[] hash = md5.ComputeHash(inputBytes);
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
        }

        public string EncodePassword(string originalPassword)
        {
            Byte[] originalBytes;
            Byte[] encodedBytes;
            System.Security.Cryptography.MD5 md5;

            // Conver the original password to bytes; then create the hash
            md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
            encodedBytes = md5.ComputeHash(originalBytes);

            // Bytes to string
            return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(encodedBytes), "-", "").ToLower();
        }

        public string GetPassword()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(RandomString(4, true));
            builder.Append(RandomNumber(1000, 9999));
            builder.Append(RandomString(2, false));
            return builder.ToString();
        }

        private int RandomNumber(int min, int max)
        {
            int ret = 0;
            Random random = new Random();
            ret = random.Next(min, max);
            random = null;
            return ret;
            
        }
        private string RandomString(int size, bool lowerCase)
        {
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }
            if (lowerCase)
                return builder.ToString().ToLower();
            random = null;
            return builder.ToString();
        }
        public string geraCupom(int _qtdcaracteres)
        {
            string ret = "";

            string A = RandomNumber(Convert.ToInt32( 1 * (Math.Pow(10, (_qtdcaracteres / 2)))), Convert.ToInt32( 9 * Math.Pow(10, (_qtdcaracteres / 2)))).ToString();
            string B = RandomString(_qtdcaracteres / 2, true);
            for (int i = 0; i < _qtdcaracteres / 2; i++)
            {
                ret = ret + A.Substring(i,1);
                ret = ret + B.Substring(i, 1);
                
            }

            return ret;
        }
    }
}
